package loppy.dynamics 
{
  import flash.display.DisplayObject;
  import flash.display.Graphics;
  import flash.display.Shape;
  import loppy.loppy_internal;
  
  public class DebugDraw 
  {
    use namespace loppy_internal;
    
    public static const FLAG_SHAPE         :uint = 0x01;
    public static const FLAG_CENTER_OF_MASS:uint = 0x02;
    public static const FLAG_AABB          :uint = 0x04;
    public static const FLAG_PAIR          :uint = 0x08;
    public static const FLAG_CONTACT       :uint = 0x10;
    
    public static const COLOR_LINE    :uint = 0xFFFFFF;
    public static const COLOR_AABB    :uint = 0x00FFFF;
    public static const COLOR_X_AXIS  :uint = 0xFF0000;
    public static const COLOR_Y_AXIS  :uint = 0x00FF00;
    public static const COLOR_CONTACT :uint = 0xFF0000;
    public static const COLOR_PAIR    :uint = 0x00FFFF;
    public static const COLOR_SLEEP   :uint = 0x008080;
    
    public static const CONTACT_RADIUS:Number = 3.0;
    public static const CONTACT_NORMAL:Number = 15.0;
    
    loppy_internal var drawScale:Number = 1.0;
    public function getDrawScale():Number { return drawScale; }
    public function setDrawScale(value:Number):DebugDraw
    {
      drawScale = value;
      return this;
    }
    
    loppy_internal var flags:uint = FLAG_SHAPE | FLAG_AABB | FLAG_CENTER_OF_MASS | FLAG_PAIR | FLAG_CONTACT;
    public function getFlags():uint { return flags; }
    public function setFlags(value:uint):DebugDraw
    {
      flags = value;
      return this;
    }
    
    public var shape:flash.display.Shape = null;
    public var graphics:Graphics         = null;
    public function getCanvas():DisplayObject { return shape; }
    
    public function DebugDraw()
    {
      shape = new flash.display.Shape();
      shape.alpha = 0.75;
      graphics = shape.graphics;
    }
    
    public function clear():void { graphics.clear(); }
    
    public function drawCircle(x:Number, y:Number, radius:Number, color:uint):void
    {
      graphics.lineStyle(0, color);
      graphics.drawCircle(x * drawScale, y * drawScale, radius * drawScale);
    }
    
    public function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, color:uint):void
    {
      graphics.lineStyle(0, color);
      graphics.moveTo(x1, y1);
      graphics.lineTo(x2, y2);
    }
    
    public function drawPolygon(vertices:Vector.<Number>, numVertices:uint, color:uint):void
    {
      graphics.lineStyle(0, color);
      graphics.moveTo(vertices[0], vertices[1]);
      for (var i:int = 1; i < numVertices; ++i)
      {
        graphics.lineTo(vertices[i << 1], vertices[i << 1 + 1]);
      }
      graphics.lineTo(vertices[0], vertices[1]);
    }
  }
}
